package xiaopiya.robot.handler.weather;

import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.VisitorSupport;

public class WebXmlWeather
{
	private String string;

	public WebXmlWeather(String response) throws DocumentException
	{
		Document doc = DocumentHelper.parseText(response);
		final StringBuffer buffer = new StringBuffer();
		final List<String> list = new ArrayList<String>();
		doc.accept(new VisitorSupport()
		{
			private boolean error = false;

			private int index = 0;

			public void visit(Element node)
			{
				if ("string".equals(node.getName()))
				{
					// 城市错误
					if (index == 0 && "查询结果为空！".equals(node.getTextTrim()))
					{
						error = true;
						buffer.append("这城市或区域暂时不被支持");
					}

					if (!error)
					{
						if (index == 10)
						{
							buffer.append(node.getTextTrim());
						}
						if (index == 5 || index == 6 || index == 12
								|| index == 13 || index == 17 || index == 18)
						{
							list.add(node.getTextTrim());
						}
					}
					index++;
				}
			}
		});
		if (list.size() >= 6)
		{
			buffer.append("\n" + list.get(1));
			buffer.append(" " + list.get(0));
			buffer.append("\n" + list.get(3));
			buffer.append(" " + list.get(2));
			buffer.append("\n" + list.get(5));
			buffer.append(" " + list.get(4));
		}
		this.string = buffer.toString();
	}

	public String toString()
	{
		return this.string;
	}
}
